import { allSkillsName, herosSkills } from '@config/hero/standard';
import { tr, trs } from '@config/tr';
import { cloneDeep, filter, includes, pullAll } from 'lodash-es';
import type { Ref } from 'vue';
import { computed, reactive } from 'vue';

interface props {
    name: Ref<string>;
    seat: Ref<seat>;
    initSkillsConfig: skillsConfig;
}
export default function useSkill(props: props): useSkillReturn {
    const skillsConfig = reactive(props.initSkillsConfig!);
    const skills = computed(() => Skill.linkSkills(skillsConfig));
    const Skill: useSkillReturn = {
        skills,
        skillsConfig,
        hasSkills(name) {
            return includes(skillsConfig, name);
        },
        addSkills(...skc) {
            const arr = filter(skc, i => {
                const bool = includes(allSkillsName, i);
                const bool2 = !includes(skillsConfig, i);
                return bool && bool2;
            });
            if (arr.length) {
                skillsConfig.push(...arr);
                console.log(`${tr(props.name.value)}获得了${trs(arr)}`);
            }
        },
        rmSkills(...skc) {
            const arr = filter(skc, i => {
                const bool = includes(allSkillsName, i);
                const bool2 = !includes(skillsConfig, i);
                return bool && !bool2;
            });
            if (arr.length) {
                pullAll(skillsConfig, ...arr);
                console.log(`${tr(props.name.value)}失去了${trs(arr)}`);
            }
        },
        clearSkills() {
            skillsConfig.splice(0, skillsConfig.length - 1);
            console.log(`${tr(props.name.value)}失去了所有技能`);
        },
        linkSkill(skillName: skillName) {
            let skill: skill = {} as skill;
            if (skillName in herosSkills) {
                skill = cloneDeep(herosSkills[skillName]) as skill;
                skill.name = skillName;
                skill.owner = props.seat.value;
                skill.global = !!skill.trigger!.global;
            }
            return skill as skill;
        },
        linkSkills(skillsConfig) {
            const skills: skills = {};
            for (const skillName of skillsConfig) {
                const skill = Skill.linkSkill(skillName);
                skills[skillName] = skill;
            }
            return skills;
        },
    };
    return Skill;
}
